<template>
  <div class="app-container">
    <!-- 搜索条件 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="智能体ID" prop="agentId">
        <el-input
          v-model="queryParams.agentId"
          placeholder="请输入ID"
          clearable
          style="width: 200px"
        />
      </el-form-item>

      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入ID"
          clearable
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格数据 -->
    <el-table
      v-loading="loading"
      :data="roleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="聊天记录ID" prop="id" width="180" />
      <el-table-column label="用户id" prop="userId" width="180" />
      <el-table-column label="智能体id" prop="agentId" width="180" />
      <el-table-column label="AI系统唯一会话ID" prop="sessionId" width="180" />
      <el-table-column label="AI系统句子ID" prop="sentenceId" width="180" />
      <el-table-column label="用户问题" prop="query" width="120" />
      <el-table-column label="AI回答" prop="answer" width="500" />
      <el-table-column label="修改后的AI回答" prop="updateAnswer" width="120" />
      <el-table-column label="用户问题图片URL" prop="userQueryImageUrl" width="120" />
      <el-table-column label="AI回答图片URL" prop="aiAnswerImageUrl" width="120" />
      <el-table-column label="用户问题音频URL" prop="userQueryAudioUrl" width="120" />
      <el-table-column label="AI回答音频URL" prop="aiAnswerAudioUrl" width="120" />
      <el-table-column label="用户问题视频URL" prop="userQueryVideoUrl" width="120" />
      <el-table-column label="AI回答视频URL" prop="aiAnswerVideoUrl" width="120" />
      <el-table-column label="评分" prop="score" width="120" />
      <el-table-column label="对话类型" prop="chatType" width="120" />
      <el-table-column label="是否修改过回答" prop="updateType" width="120" />
      <el-table-column label="消息时间戳" prop="messageTimestamp" width="120" />
      <el-table-column label="消息时间" prop="messageTime" width="120" />
      <el-table-column label="修改时间" align="center" prop="updateTime">
      <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <!-- 操作 -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip
            content="查看聊天记录"
            placement="top"
            v-if="scope.row.isAdmin !== 1"
          >
            <el-button
              link
              type="primary"
              icon="User"
              @click="handleAuthUser(scope.row)"
              v-hasPermi="['admin:role:relevancyDepts']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />

    <!-- 弹窗 -->
    <el-dialog title="聊天记录" v-model="open" width="600px" append-to-body>
      <div>
        <p>角色编号：{{ currentRow.id }}</p>
        <p>query：{{ currentRow.query }}</p>
        <p>answer：{{ currentRow.answer }}</p>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Role">
import { listRole } from "@/api/min-app/chathistory";
const { proxy } = getCurrentInstance();

const roleList = ref([]);
const loading = ref(true);
const total = ref(0);

const open = ref(false);         // 控制弹窗
const currentRow = ref({});      // 当前点击的行数据

// 查询参数
const data = reactive({
  form: {},
  queryParams: {
    page: 1,
    size: 10,
    agentId: '',
    userId: '',
  },
});
const { queryParams, form } = toRefs(data);

// 查询列表
function getList() {
  loading.value = true;
  const params = {
    page: queryParams.value.page,
    size: queryParams.value.size,
    agentId: queryParams.value.agentId || '-1',
    userId: queryParams.value.userId ||'-1',
  };
  listRole(params).then((res) => {
    roleList.value = res.data.records;
    total.value = Number(res.data.total); // 转数字
    loading.value = false;
  });
}

// 搜索
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}

// 重置
function resetQuery() {
  queryParams.value.page = 1;
  queryParams.value.userId = '';
  queryParams.value.agentId = '';
  getList();
}

// 表格多选
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

// 点击“查看聊天记录”
function handleAuthUser(row) {
  currentRow.value = row;
  open.value = true;
}

// 取消弹窗
function cancel() {
  open.value = false;
}

// 模拟提交
function submitForm() {
  proxy.$modal.msgSuccess("提交成功！");
  open.value = false;
}

// 初始化加载
getList();
</script>
